import { getHostList, updateHostAppList } from "@/api/vap/cluster";
import { reactive, ref, computed, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

export function useHost() {
  const dialogFormAddRef = ref();
  const dialogFormTrimRef = ref();

  const dataList = ref([]);
  const loading = ref(true);

  const form = reactive({
    name: ""
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      minWidth: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "主机UID",
      prop: "uid",
      align: "center"
    },
    {
      label: "主机名称",
      prop: "hostName",
      align: "center"
    },
    {
      label: "IP地址",
      prop: "ip",
      align: "center"
    },
    {
      label: "操作系统",
      prop: "osCaption",
      align: "center"
    },
    {
      label: "RDP",
      prop: "rdpPort",
      align: "center"
    },
    {
      label: "域",
      prop: "domain",
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={row.partOfDomain ? "success" : "info"}
          effect="plain"
        >
          {row.domain}
        </el-tag>
      )
    },
    {
      label: "系统类型",
      prop: "os",
      cellRenderer: ({ row, props }) => (
        <el-tag size={props.size} type="success" effect="plain">
          {row.os}
        </el-tag>
      )
    },
    {
      label: "来源",
      prop: "origin",
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={
            row.status === 1 ? "success" : row.status === 2 ? "info" : "danger"
          }
          effect="plain"
        >
          {row.status === 1 ? "客户端" : row.status === 2 ? "docker" : "其他"}
        </el-tag>
      )
    },
    {
      label: "状态",
      prop: "status",
      minWidth: 100,
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={
            row.status === 1 ? "success" : row.status === 2 ? "info" : "danger"
          }
          effect="plain"
        >
          {row.status === 1 ? "正常" : row.status === 2 ? "维护" : "禁用"}
        </el-tag>
      )
    },
    {
      label: "操作",
      fixed: "right",
      width: 160,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });

  function handleDelete(row: any) {
    console.log(row);
    // deleteHost(row.id).then(() => {
    //   onSearch();
    // });
  }
  function handleUpdateHostAppsClick(row: any) {
    ElMessageBox.prompt("请输入应用图标目录", "Tip", {
      confirmButtonText: "提交",
      cancelButtonText: "取消",
      inputValue: "C:\\app"
    })
      .then(({ value }) => {
        updateHostAppList({ hostId: row.id, iconPath: value });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消操作"
        });
      });
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }
  function handlerOption(row: any) {
    console.log(row);
  }
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  async function onSearch() {
    loading.value = true;
    getHostList().then(res => {
      dataList.value = res;
      setTimeout(() => {
        loading.value = false;
      }, 500);
    });
  }

  onMounted(() => {
    onSearch();
  });

  return {
    dialogFormAddRef,
    dialogFormTrimRef,
    loading,
    form,
    columns,
    dataList,
    buttonClass,
    onSearch,
    resetForm,
    handlerOption,
    handleSelectionChange,
    handleDelete,
    handleUpdateHostAppsClick
  };
}
